package com.dfbz.controller.article;

import com.dfbz.controller.BaseServlet;
import com.dfbz.model.Article;
import com.dfbz.model.Favorite;
import com.dfbz.model.User;
import com.dfbz.pojo.PageEntity;
import com.dfbz.service.ArticleService;
import com.dfbz.service.FavoriteService;
import com.dfbz.service.UserService;
import com.dfbz.service.impl.ArticleServiceImpl;
import com.dfbz.service.impl.FavoriteServiceImpl;
import com.dfbz.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;

@WebServlet("/article/*")
public class ArticleServlet extends BaseServlet {
    ArticleService articleService=new ArticleServiceImpl();
    UserService userService=new UserServiceImpl();
    FavoriteService favoriteService=new FavoriteServiceImpl();
    //文章
    public void article(HttpServletRequest req, HttpServletResponse resp){
        try {
            HttpSession session = req.getSession();
            String p=req.getParameter("currPage");
            String articleTitle=req.getParameter("articleTitle");
            Integer currPage;
            if (p==null){
                currPage=1;
            }
            else {
                currPage=Integer.parseInt(p);
            }
            if (articleTitle ==null){
                if (null !=session.getAttribute("articleTitle")){
                    articleTitle=(String) session.getAttribute("articleTitle");
                }
                else {
                    articleTitle="";
                }
            }
            User userInfo = (User) session.getAttribute("userInfo");
            session.setAttribute("articleTitle",articleTitle);
            PageEntity<Article> ArticlepageEntity = articleService.findPage(userInfo.getId(), currPage, articleTitle);
            session.setAttribute("ArticlepageEntity",ArticlepageEntity);
            req.getRequestDispatcher("/view/location/article").forward(req,resp);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void article_next(HttpServletRequest req, HttpServletResponse resp){
        try {
            HttpSession session = req.getSession();
            User userInfo = (User) session.getAttribute("userInfo");
            PageEntity<Article> articlepageEntity = (PageEntity<Article>) session.getAttribute("ArticlepageEntity");
            String articleTitle =(String) req.getSession().getAttribute("articleTitle");
            Integer currPage = articlepageEntity.getCurrPage();
            Integer end = articlepageEntity.getEnd();
            Integer totalPage = articlepageEntity.getTotalPage();
            if (currPage==end){
                if (currPage==totalPage){
                    req.getRequestDispatcher("/view/location/article").forward(req,resp);
                    return;
                }
                else {
                    currPage+=1;
                    PageEntity<Article> page = articleService.findPage(userInfo.getId(), currPage, articleTitle);
                    req.getSession().setAttribute("ArticlepageEntity",page);
                    req.getRequestDispatcher("/view/location/article").forward(req,resp);
                    return;
                }
            }
            else {
                currPage+=1;
                PageEntity<Article> page = articleService.findPage(userInfo.getId(), currPage, articleTitle);
                req.getSession().setAttribute("ArticlepageEntity",page);
                req.getRequestDispatcher("/view/location/article").forward(req,resp);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void article_back(HttpServletRequest req, HttpServletResponse resp){
        try {
            HttpSession session = req.getSession();
            User userInfo = (User) session.getAttribute("userInfo");
            PageEntity<Article> articlepageEntity = (PageEntity<Article>) session.getAttribute("ArticlepageEntity");
            String articleTitle =(String) req.getSession().getAttribute("articleTitle");
            Integer currPage = articlepageEntity.getCurrPage();
            Integer begin = articlepageEntity.getBegin();
            if (begin==currPage){
                if (currPage==1){
                    req.getRequestDispatcher("/view/location/article").forward(req,resp);
                    return;
                }
                else {
                    currPage-=1;
                    PageEntity<Article> page = articleService.findPage(userInfo.getId(), currPage, articleTitle);
                    req.getSession().setAttribute("ArticlepageEntity",page);
                    req.getRequestDispatcher("/view/location/article").forward(req,resp);
                    return;
                }
            }
            else {
                currPage-=1;
                PageEntity<Article> page = articleService.findPage(userInfo.getId(), currPage, articleTitle);
                req.getSession().setAttribute("ArticlepageEntity",page);
                req.getRequestDispatcher("/view/location/article").forward(req,resp);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //添加文章
    public void article_add(HttpServletRequest req, HttpServletResponse resp){
        try {
            HttpSession session = req.getSession();
            String title = req.getParameter("title");
            String content = req.getParameter("content");
            User userInfo = (User) session.getAttribute("userInfo");
            ArrayList<Article> list = articleService.findList();
            Article article1 = list.get(list.size() - 1);
            Integer article_id=article1.getId()+1;
            Date date=new Date();
            Article article=new Article();
            article.setId(article_id);
            article.setTitle(title);
            article.setContent(content);
            article.setBrowseCount(0);
            article.setPublishDate(date);
            if (userInfo.getRealName()==null){
                article.setPublishRealName(userInfo.getUsername());
            }
            else {
                article.setPublishRealName(userInfo.getRealName());
            }
            article.setUserId(userInfo.getId());
            articleService.save(article);
            req.getRequestDispatcher("/view/location/article").forward(req,resp);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void article_detail(HttpServletRequest req, HttpServletResponse resp){
        try {
            HttpSession session = req.getSession();
            User userInfo = (User) session.getAttribute("userInfo");
            Integer articleId=Integer.parseInt(req.getParameter("articleId"));
            Article article = articleService.findById(articleId);
            //收藏数
            Integer favCount = articleService.countById(articleId);
            //判断是否收藏
            String IsfavArticle="no";
            Integer favorites = articleService.IsfavArticle(userInfo.getId(), articleId);
            if (favorites !=0){
                IsfavArticle="yes";
            }
            //也收藏该文章的朋友
            ArrayList<User> favArtUsers=new ArrayList<>();
            ArrayList<Favorite> favArticles = articleService.favArticleById(articleId, userInfo.getId());
            for (Favorite favorite:favArticles) {
                Integer uId = favorite.getUId();
                User user = userService.findById(uId);
                favArtUsers.add(user);
            }
            session.setAttribute("article",article);
            session.setAttribute("favCount",favCount);
            session.setAttribute("IsfavArticle",IsfavArticle);
            session.setAttribute("favArtUsers",favArtUsers);
            req.getRequestDispatcher("/view/location/article_detail").forward(req,resp);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void fav(HttpServletRequest req, HttpServletResponse resp){
        try {
            HttpSession session = req.getSession();
            User userInfo = (User) session.getAttribute("userInfo");
            Integer artId=Integer.parseInt(req.getParameter("artId"));
            //判断是否收藏
            String IsfavArticle="no";
            Integer favorites = articleService.IsfavArticle(userInfo.getId(), artId);
            if (favorites !=0){
                IsfavArticle="yes";
            }
            if (IsfavArticle.equals("yes")){
                //取消收藏
                favoriteService.deleteFav(userInfo.getId(),artId);
            }
            else {
                //收藏
                Favorite favorite=new Favorite();
                favorite.setUId(userInfo.getId());
                favorite.setAId(artId);
                favoriteService.save(favorite);
            }
            req.getRequestDispatcher("/article/article_detail?articleId="+artId).forward(req,resp);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
